﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Collections.ObjectModel;
using SurveyControl.Controls;

namespace SurveyControl
{
    public class Survey : INotifyPropertyChanged
    {
        #region Properties

        #region CurrentIndex

        public const string CurrentIndexPropertyName = "CurrentIndex";

        protected int _CurrentIndex = 0;

        public int CurrentIndex
        {
            get { return _CurrentIndex; }
            set
            {
                //if (_CurrentIndex == value)
                //    return;

                _CurrentIndex = value;

                if (_Questions.Count != 0)
                    CurrentQuestion = _Questions[_CurrentIndex];
                else
                    CurrentQuestion = null;

                RaisePropertyChanged(CurrentIndexPropertyName);
                RaisePropertyChanged(HasPreviousPropertyName);
                RaisePropertyChanged(HasNextPropertyName);

            }
        }

        #endregion

        #region CurrentQuestion

        public const string CurrentQuestionPropertyName = "CurrentQuestion";
        
        protected object _CurrentQuestion = null;

        public object CurrentQuestion
        {
            get { return _CurrentQuestion; }
            set 
            {
                if (_CurrentQuestion == value)
                    return;
                    
                _CurrentQuestion = value;
                RaisePropertyChanged(CurrentQuestionPropertyName);

            }
        }

        #endregion

        #region Questions

        public const string QuestionsPropertyName = "Questions";

        protected ObservableCollection<QuestionControl> _Questions = null;

        public ObservableCollection<QuestionControl> Questions
        {
            get { return _Questions; }
            set
            {
                if (_Questions == value)
                    return;

                _Questions = value;
                CurrentIndex = 0;
                RaisePropertyChanged(QuestionsPropertyName);

            }
        }

        #endregion

        #region HasNext

        public const string HasNextPropertyName = "HasNext";

        public bool HasNext
        {
            get 
            {
                if (_Questions == null)
                    return false;
                return ((_Questions.Count - 1) > _CurrentIndex);
            }
        }

        #endregion

        #region HasPrevious

        public const string HasPreviousPropertyName = "HasPrevious";

        public bool HasPrevious
        {
            get { return (_Questions != null && (_CurrentIndex > 0)); }
        }

        #endregion


        #endregion

        public Survey()
        {

        }

        #region Methode

        public void Next()
        {
            if (HasNext)
            CurrentIndex = CurrentIndex + 1;
        }

        public void Previous()
        {
            if (HasPrevious)
                CurrentIndex = CurrentIndex - 1;
        }

        public void Submit()
        {

        }
        #endregion

        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
